package com.pg.agent.engine.client.message;

import com.alibaba.fastjson.JSONObject;
import com.pg.agent.engine.client.dto.AgentExeDTO;
import com.pg.agent.engine.client.model.SessionObject;
import com.pg.agent.engine.client.model.agent.AIAgent;
import com.pg.agent.engine.client.model.environment.AIEnvironment;
import com.pg.agent.engine.client.model.llm.CallLog;
import com.pg.agent.engine.client.model.sop.AISOP;
import com.pg.agent.engine.client.model.state.AIState;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
public class InPutMessage {

    /**
     * 当前会话上下文环境
     */
    private AIEnvironment environment;
    /**
     * 当前会话的SOP
     */
    private AISOP sop;
    /**
     * 当前会话执行的state
     */
    private AIState state;
    /**
     * 当前会话执行的agent
     */
    private AIAgent agent;
    /**
     * 会话ID sessionId
     */
    private String sessionId;
    /**
     * 执行ID
     */
    private Integer exeId;

    /**
     * 获取数据的关键字
     */
    private String userKey;

    /**
     *消息Id
     */
    private String messageId;

    /**
     * 提问
     */
    private String query;

    /**
     * 是否调试
     */
    private boolean debug = false;
    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 本次会话执行，调用大模型日志
     */
    private List<CallLog> callLogList = new ArrayList<>();


    /**
     * 图片集合 格式[{"id":"","type":1,"name":xxx,"url":xxx,"desc":xxx,"otherInfo":xxx}]
     */
    private String imageList;

    /**
     * 文件集合 格式[{"id","type":1,"name":xxx,"url":xxx,"desc":xxx,"otherInfo":xxx}]
     */
    private String fileList;

    /**
     * 标签集合
     */
    private String labelList;

    /**
     * 用户自定义参数
     */
    private JSONObject  userParam;


    private Integer saasAppId;

    private Integer exeType;

    private Integer type;

    private boolean repeat;

    public InPutMessage(AIEnvironment environment, AISOP sop, String sessionId, Integer exeId) {
        this.environment = environment;
        this.sop = sop;
        this.sessionId = sessionId;
        this.exeId = exeId;
    }

    public InPutMessage(AIEnvironment environment, SessionObject sessionObject) {
        this.environment = environment;
        this.sop = sessionObject.getAiSOP();
        this.sessionId = sessionObject.getSessionId();
        this.exeId = sessionObject.getExeNum();
        this.userId = sessionObject.getUserId();
    }

    public InPutMessage(AIEnvironment environment, SessionObject sessionObject, JSONObject userParam) {
        this.environment = environment;
        this.sop = sessionObject.getAiSOP();
        this.sessionId = sessionObject.getSessionId();
        this.exeId = sessionObject.getExeNum();
        this.userId = sessionObject.getUserId();
        this.userParam = userParam;
    }

    public InPutMessage(AIEnvironment environment, SessionObject sessionObject, Boolean debug) {
        this.environment = environment;
        this.sop = sessionObject.getAiSOP();
        this.sessionId = sessionObject.getSessionId();
        this.exeId = sessionObject.getExeNum();
        this.userId = sessionObject.getUserId();
        this.debug = debug;
    }

    public InPutMessage(AIEnvironment environment, SessionObject sessionObject, String userKey, AgentExeDTO runSopDTO) {
        this.environment = environment;
        this.sop = sessionObject.getAiSOP();
        this.sessionId = sessionObject.getSessionId();
        this.exeId = sessionObject.getExeNum();
        this.userId = sessionObject.getUserId();
        this.userKey = userKey;
        this.debug = runSopDTO.isDebug();
        this.query = runSopDTO.getContent();
        this.userParam = runSopDTO.getUserParam();
        this.imageList = runSopDTO.getImageList();
        this.fileList = runSopDTO.getFileList();
    }

    public InPutMessage() {
    }

    /**
     * 获取本次对话记录ID
     *
     * @return
     */
    public String getDataId() {
        return sessionId + "-" + exeId;
    }

    public void addCallLog(CallLog callLog, String callResponse) {

        callLog.setCallEndTime(new Date());
        callLog.setTimeLong(System.currentTimeMillis() - callLog.getCallStartTime().getTime());
        callLog.setResponse(callResponse);

        callLogList.add(callLog);
    }

}
